/**
 * Created by Administrator on 2017/5/8 0008.
 */
//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element();

    //…
});

layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage, layer = layui.layer;
    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function (obj, first) {
            //得到了当前页，用于向服务端请求对应数据
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                if (jsonObject.length > 0) {
                    console.log(jsonObject);
                    var objs = eval('(' + jsonObject + ')');//将json转成对象
                    var condition = 'pageIndex=' + pageIndex + '&';
                    condition += "couponsVo.type=" + objs.type + '&' + "couponsVo.status=" + objs.status + '&' +
                        "couponsVo.description=" + objs.description + '&' + "couponsVo.startvalue=" + objs.startvalue + '&' +
                        "couponsVo.endvalue=" + objs.endvalue + '&' + "couponsVo.startdate=" + objs.startdate + '&' +
                        "couponsVo.enddate=" + objs.enddate;
                    console.log(condition);
                    window.location.href = "/admin/coupons/querylist?" + condition;
                } else {
                    window.location.href = "/admin/coupons/showdetail?pageIndex=" + pageIndex;
                }
            }
        }
    });
});

layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage, layer = layui.layer;
    //地址分页
    laypage({
        cont: 'detailpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function (obj, first) {
            //得到了当前页，用于向服务端请求对应数据
            var cusid = $('#croCustomerid').val();
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                window.location.href = "/admin/coupons/?pageIndex=" + pageIndex + '&customerid=' + cusid;
            }
        }
    });
});



layui.use('laydate', function () {
    var laydate = layui.laydate;
    var start = {
        min: laydate.now()
        , max: '2099-06-16 23:59:59'
        , istoday: false
        , choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };

});
layui.use('form', function () {
    var $ = layui.jquery, form = layui.form();

    //全选
    form.on('checkbox(allChoose)', function (data) {
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
        child.each(function (index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });
    form.on('submit(sumit)', function (data) {
        layer.alert(JSON.stringify(data.field), {
            title: '最终的提交信息'
        })
        return false;
    });
    //监听提交
    form.on('submit(fromsubmit)', function (data) {
        console.log(data.field);
        var params = $.param({
            type: data.field.type,
            value: data.field.value,
            uplimit: data.field.uplimit,
            description: data.field.description
        }, true);
        // console.log(params);
        var value = $('#value').val();
        var uplimit = $('#uplimit').val();

        function checkRates(str) {
            var r = /^\d+(\.\d+)?$/
            var Sure;
            if (!r.test(str)) {
                Sure = 0;//错误为0
            } else {
                Sure = 1;
            }
            return Sure;
        }

        if (uplimit - value > 0 && checkRates(value) && checkRates(uplimit)) {
            $.ajax({
                url: '/addcoupons.action',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    window.location.href = "/admin/coupons/showdetail";
                    layer.msg("修改成功");
                }
            });
        } else {
            layer.msg("请检查优惠券限额是否大于优惠券价值,或者优惠券限额和价值是否正整数");
        }
        return false;
    });


    form.on('submit(updatecoupons)', function (data) {
        console.log(data.field);
        var params = $.param({
            id: data.field.id,
            type: data.field.upcroCouponstype,
            value: data.field.value,
            uplimit: data.field.uplimit,
            description: data.field.description,
            status: data.field.upcroCouponsstauts,
            offdate: data.field.offdate,
        }, true);

        var value = $('#value').val();
        var uplimit = $('#uplimit').val();
        var status = $('#upcroCouponsstauts').val();
        var offdata = $('#offdata').val();
        console.log(checkRates(value));

        function checkRates(str) {
            var r = /^\d+(\.\d+)?$/;
            var Sure;
            if (!r.test(str)) {
                Sure = 0;//错误为0
            } else {
                Sure = 1;
            }
            return Sure;
        }


        if (uplimit - value > 0 && checkRates(value) && checkRates(uplimit)) {
            $.ajax({
                url: '/updatecoupons.action',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (data) {
                    parent.window.location.reload();
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                    layer.msg("修改成功");
                }
            });
        } else {
            layer.msg("请检查优惠券限额是否大于优惠券价值,或者优惠券限额和价值是否正整数");
        }

        return false;
    });

});

//layer弹出层
layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件

    var active = {
        update: function (othis) {
            //弹出即全屏
            var index = layer.open({
                type: 2,
                title: "修改优惠券",
                shadeClose: true,
                content: 'listCoupons?id=' + othis.couponsid,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        },


        issue: function (othis) {
            var content = $('#issuecoupons');
            layer.open({
                type: 1,
                title: '发放优惠券',
                skin: 'layui-layer-molv', //样式类名
                closeBtn: false, //不显示关闭按钮
                anim: 2,
                area: '800px;',
                btn: ['绝不后悔', '我怂了'],
                shadeClose: true, //开启遮罩关闭
                content: content,
                success: function (layero) {
                    var userInfoParent = $(othis).parents('tr');
                    var userInfoItem = userInfoParent.children("td");
                    $('#cpid').val(userInfoItem[1].innerText);
                    $('#status').val(userInfoItem[2].innerText);
                },

                yes: function (index, layero) {
                    var params = $.param({
                        offdate: $('#LAY_demorange_s').val(),
                        id: $('#cpid').val(),
                        number: $('#number').val(),
                    }, true);
                    console.log(params);
                    var status = $('#cstatus').val();
                    if (status != "已发放") {
                    }
                    $.ajax({
                        url: '/issuecoupons.action',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                            if (data == "修改成功") {
                                window.location.href = "/admin/coupons/showdetail";//重新加载该页面
                                layer.msg("发放成功");
                            }
                        }

                    })

                }
            });
        },
        delete: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/deletecoupons.action",
                        type: 'post',
                        data: {
                            id: othis.couponsid,
                            del_flag: "1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "删除成功") {
                                window.location.href = "/admin/coupons/showdetail";
                                layer.msg("删除成功");
                            } else {
                                layer.msg("删除失败");
                            }
                        }
                    });

                }
            });
        },
    };
    $('.delsumit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.issuecoupons').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.couponssumit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.updatecoupons').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.updatesumit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

